package com.itjeffrey.autocode.controller;

import com.itjeffrey.autocode.bo.req.MockInterfaceBO;
import com.itjeffrey.autocode.common.Result;
import com.itjeffrey.autocode.service.MockInterfaceTbService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * MOCK接口控制器类
 *
 * @author: admin
 * @date: 2020-12-28 23:58:06
 */
@RestController
@RequestMapping("/mock/interface")
public class MockInterfaceTbController {

    @Resource
    private MockInterfaceTbService mockInterfaceTbService;

    /**
     * 接口新增
     *
     * @param mockInterfaceBO
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity<Result> add(@RequestBody @Valid MockInterfaceBO mockInterfaceBO) {
        return ResponseEntity.ok(mockInterfaceTbService.add(mockInterfaceBO));
    }

    /**
     * 接口修改，只修改请求参数格式，响应参数格式
     *
     * @param mockInterfaceBO
     * @return
     */
    @PostMapping("/mod")
    public ResponseEntity<Result> mod(@RequestBody @Valid MockInterfaceBO mockInterfaceBO) {
        return ResponseEntity.ok(mockInterfaceTbService.mod(mockInterfaceBO));
    }

    /**
     * 接口删除
     *
     * @return
     */
    @PostMapping("/del/{id}")
    public ResponseEntity<Result> del(@PathVariable("id") String id) {
        return ResponseEntity.ok(mockInterfaceTbService.del(id));
    }

    /**
     * 根据Id查询单个接口详情
     *
     * @return
     */
    @PostMapping("/query/{id}")
    public ResponseEntity<Result> queryOne(@PathVariable("id") String id) {
        return ResponseEntity.ok(mockInterfaceTbService.queryOne(id));
    }

    /**
     * 根据条件查询接口列表
     *
     * @return
     */
    @PostMapping("/query/list")
//    @PreAuthorize("hasPermission('/mock/interface/query/list', '/mock/interface/query/lista')")
    public ResponseEntity<Result> queryList(@RequestBody MockInterfaceBO mockInterfaceBO) {
        return ResponseEntity.ok(mockInterfaceTbService.queryList(mockInterfaceBO));
    }

    /**
     * 接口mock调试入口
     *
     * @param mockInterfaceBO
     * @return
     */
    @PostMapping("/debug")
    public ResponseEntity<Result> debugEntry(@RequestBody MockInterfaceBO mockInterfaceBO) {
        return ResponseEntity.ok(mockInterfaceTbService.debugEntry(mockInterfaceBO));
    }

    /**
     * 接口列表数据导出
     *
     * @param mockInterfaceBOS
     * @param response
     * @return
     */
    @PostMapping("/export")
    public ResponseEntity<Void> itfExport(@RequestBody List<MockInterfaceBO> mockInterfaceBOS, HttpServletResponse
            response) {
        mockInterfaceTbService.itfExport(mockInterfaceBOS, response);
        return ResponseEntity.ok().build();
    }
}